package com.sxbang.bms.controller;

import com.alibaba.fastjson.JSONArray;
import com.sxbang.bms.base.result.Results;
import com.sxbang.bms.dto.RoleDto;
import com.sxbang.bms.model.SysPermission;
import com.sxbang.bms.page.PageTableRequest;
import com.sxbang.bms.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("permission")
@Slf4j
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    @GetMapping
    @RequestMapping("/listPage")
    @ResponseBody
    public Results list(PageTableRequest request) {
        request.countOffset();
        return permissionService.listByPage(request.getOffset(),request.getLimit());
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String editUser(Model model, SysPermission permission) {
        model.addAttribute("sysPermission",permissionService.get(permission.getId()));
        return "role/role-edit";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Results<SysPermission> updateRole(SysPermission permission) {
        permissionService.update(permission);
        return Results.success();
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addUser(Model model) {
        model.addAttribute("sysPermission",new SysPermission());
        return "role/role-add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Results<SysPermission> saveRole(SysPermission permission) {
        permissionService.save(permission);
        return Results.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public Results<SysPermission> deleteRole(RoleDto roleDto) {
        permissionService.delete(roleDto.getId());
        return Results.success();
    }

    @RequestMapping(value = "/listAllPermission", method = RequestMethod.GET)
    @ResponseBody
    public Results<JSONArray> listAllPermission(RoleDto roleDto) {
        return permissionService.listAllPermission();
    }

    @RequestMapping(value = "/listAllPermissionByRoleId", method = RequestMethod.GET)
    @ResponseBody
    public Results<SysPermission> listAllPermissionByRoleId(RoleDto roleDto) {
        log.info(getClass().getName() + ".listAllPermissionByRoleId()");
        return permissionService.listByRoleId(roleDto.getId().intValue());
    }
}
